#ifndef _SOURCE_IO_H_
#define _SOURCE_IO_H_

#include "Common.h"
#include "Source.h"
#include "VolumeSource.h"
#include "PipelineModel.h"
#include "LoadDicomSeriesThread.h"

#include <QFileDialog>
#include <QString>
#include <QDebug>

#include "itkImage.h"
#include "itkImageFileWriter.h"

class SourceIO : public QObject
{
	Q_OBJECT

public:
	SourceIO(PipelineModel *pm);
	~SourceIO();

	void loadDicomSeries(const QString &dirname, const QString &sourceLabel);
	void loadFile(const QString& filename, const QString& label);

	static void write(Source *source);

private slots:
	void slotProgressDone();

signals:
	void emitProgressStart(QString);
	void emitProgressUpdate(int);
	void emitProgressDone();


private:
	int m_currDataType;
	QString m_currFileName;
	QString m_currSourceLabel;
	PipelineModel *m_pipelineModel;
	LoadDataThreadBase *m_loadDataThread;


	template<class FPixelType>
	void createloadDicomSeriesThread()
	{
		typedef itk::Image<FPixelType, 3> FImageType;
		m_loadDataThread = new LoadDicomSeriesThread<FImageType>(m_currFileName, m_pipelineModel);

		connect(m_loadDataThread, SIGNAL(emitProgressStart(QString)), SIGNAL(emitProgressStart(QString)));
		connect(m_loadDataThread, SIGNAL(emitProgressUpdate(int)), SIGNAL(emitProgressUpdate(int)));
		connect(m_loadDataThread, SIGNAL(emitProgressDone()), SLOT(slotProgressDone()));

		m_loadDataThread->start();
	}


	template<class FPixelType>
	void createBaseSourceFromThread()
	{
		typedef itk::Image<FPixelType, 3> FImageType;
		typename FImageType::Pointer image = ((LoadDataThread<FImageType>*)m_loadDataThread)->getOutput();

		VolumeSource<FImageType> *source = 
			new VolumeSource<FImageType>(QString("%1").
			arg(m_currSourceLabel));

		source->setVolume(image);
		source->setFileName(m_currFileName);
		m_pipelineModel->addSource(source);
	}

	
	template<class FPixelType>
	void createloadVtkFileThread()
	{
		typedef itk::Image<FPixelType, 3> FImageType;

        /*
        typedef itk::ImageFileReader<FImageType> ImageFileReader;
        typename ImageFileReader::Pointer reader = ImageFileReader::New();
        
        reader->SetFileName(m_currFileName.toLatin1().data());
        reader->Update();
        return;
        
        */
        
        
		m_loadDataThread = new LoadVtkFileThread<FImageType>(m_currFileName, m_pipelineModel);

		connect(m_loadDataThread, SIGNAL(emitProgressStart(QString)), SIGNAL(emitProgressStart(QString)));
		connect(m_loadDataThread, SIGNAL(emitProgressUpdate(int)), SIGNAL(emitProgressUpdate(int)));
		connect(m_loadDataThread, SIGNAL(emitProgressDone()), SLOT(slotProgressDone()));

		m_loadDataThread->start();
	}


	template <class FPixelType>
	static void writeVolume(Source *source, QString fileName)
	{
		typedef itk::Image<FPixelType, 3> FImageType;
		typedef itk::ImageFileWriter<FImageType> ImageFileWriter;

		VolumeSource<FImageType> *volume = (VolumeSource<FImageType>*)source;

		typename ImageFileWriter::Pointer writer = ImageFileWriter::New();
		writer->SetInput(volume->getVolume());
		//writer->Set
		writer->SetFileName(fileName.toLatin1().data());
		writer->Update();
	}

	
// 	static void writeSurface(Source* source, QString fileName)
// 	{
// 		
// 	}

};





#endif
